package com.scx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scx.constant.MessageConstant;
import com.scx.constant.PasswordConstant;
import com.scx.constant.StatusConstant;
import com.scx.context.BaseContext;
import com.scx.dto.EmployeeDTO;
import com.scx.dto.EmployeeLoginDTO;
import com.scx.dto.EmployeePageQueryDTO;
import com.scx.entity.Employee;
import com.scx.exception.AccountLockedException;
import com.scx.exception.AccountNotFoundException;
import com.scx.exception.PasswordErrorException;
import com.scx.mapper.EmployeeMapper;
import com.scx.result.PageResult;
import com.scx.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Objects;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //根据用户名查询数据库中的数据
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, username);
        Employee employee = employeeMapper.selectOne(lqw);
//        Employee employee = employeeMapper.getByUsername(username);


        //处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (Objects.equals(employee.getStatus(), StatusConstant.DISABLE)) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO, employee);
        //初始化状态
        employee.setStatus(StatusConstant.ENABLE);
        //初始化密码
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        employeeMapper.insert(employee);
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        Page<Employee> page = new Page<>(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        employeeMapper.selectByNamePage(page, employeePageQueryDTO.getName());
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Employee employee = Employee.builder().id(id).status(status).build();
        System.out.println(employee);
        System.out.println(employeeMapper.updateById(employee));
    }

    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.selectById(id);
        employee.setPassword("******");
        return employee;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);

        employeeMapper.updateById(employee);
    }
}
